package com.lyy.lottery.common.interceptor;

import com.lyy.lottery.common.util.JWTUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求头
        String token=request.getHeader("user_token");
        log.info("获取token：{}",token);
        log.info("获取路径：{}",request.getRequestURI());
        //令牌解析
        Claims claims= JWTUtil.parseJWT(token);
        if (null==claims){
            log.error("令牌解析失败!");
            response.setStatus(401);
            return false;
        }
        log.info("解析令牌成功！放行");
        return true;
    }
}























